﻿Imports System.Data.OleDb
Imports System
Imports System.Data
Imports System.Xml
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic

Public Class Site
    Inherits System.Web.UI.MasterPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Me.Page.User.IsInRole("Admin") Then
            NavigationMenu.Items.Add(New MenuItem("User List", "User List", Nothing, "~/UserList.aspx"))
            NavigationMenu.Items.Add(New MenuItem("Stats", "Stats", Nothing, "~/Stats.aspx"))
        End If

        If Me.Page.User.IsInRole("UserPlus") Then
            NavigationMenu.Items.Add(New MenuItem("Stats", "Stats", Nothing, "~/Stats.aspx"))
        End If

        If Me.Page.User.Identity.Name <> "" Then
            If Not isActive() Then
                If Session("Registering") <> "Yes" Then
                    FormsAuthentication.SignOut()
                    Response.Redirect("~/UserList.aspx")
                End If
            End If

        End If

        If Not IsPostBack Then
            LoadWeather()
            imgHeader.ImageUrl = Page.ResolveUrl("~") + "Styles/header.jpg"
        End If
    End Sub

    Protected Function isActive() As Boolean

        Dim returnValue As Boolean

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        Dim SqlString As String = "SELECT Active FROM Users WHERE UserName = @param1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@param1", Me.Page.User.Identity.Name)
        oleDbConn.Open()

        Dim userData As OleDbDataReader = cmd.ExecuteReader()

        If userData.HasRows Then
            Do While userData.Read()

                If Not userData("Active") Is DBNull.Value Then
                    returnValue = userData.GetBoolean(0)
                End If

            Loop
        End If

        Return returnValue

    End Function


    Protected Sub LoadWeather()

        Dim RSS As String = "http://weather.yahooapis.com/forecastrss?w=24153215&u=c"

        Dim xDoc As XmlDocument = New XmlDocument()
        xDoc.Load(RSS)

        Dim ItemList As XmlNodeList = xDoc.GetElementsByTagName("item")
        Dim Title As String = ItemList(0).SelectSingleNode("title").InnerText
        Dim Description As String = ItemList(0).SelectSingleNode("description").InnerText

        Dim imageWeather As String = ""
        Dim x As Integer = Description.IndexOf("<img src") + 10
        Dim y As Integer = Description.IndexOf("gif")
        imageWeather = Description.Substring(x, y - x + 3)

        Dim currentWeather As String = ""
        x = Description.IndexOf("Current") + 19
        y = Description.IndexOf("Forecast")
        currentWeather = Description.Substring(x, y - x)

        currentWeather = Replace(currentWeather, "<BR />", "")
        currentWeather = Replace(currentWeather, "<br />", "")
        currentWeather = Replace(currentWeather, "</b>", "")
        currentWeather = Replace(currentWeather, "<b>", "")

        lblInfo.Text = Title
        lblWeather.Text = currentWeather
        imgWeather.ImageUrl = imageWeather

    End Sub



End Class